package edu.qqilin.springframework.context.support;

import edu.qqilin.springframework.beans.BeansException;
import edu.qqilin.springframework.beans.factory.ConfigurableListableBeanFactory;
import edu.qqilin.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * Description: 负责维护 BeanFactory 的中间层
 * <p>
 * 核心功能：
 * <p>
 * 实现了 refreshBeanFactory()：每次刷新时，创建一个新的 DefaultListableBeanFactory。
 * <p>
 * 定义了抽象方法 loadBeanDefinitions，强制子类必须去实现“怎么加载 Bean 定义”。
 * @author qqilin1213
 * @version 1.0
 * @date 2025/11/24 20:37
 */
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {

    private DefaultListableBeanFactory beanFactory;

    @Override
    protected void refreshBeanFactory() throws BeansException {
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        loadBeanDefinitions(beanFactory);
        this.beanFactory = beanFactory;
    }

    private DefaultListableBeanFactory createBeanFactory() {
        return new DefaultListableBeanFactory();
    }

    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory);

    @Override
    protected ConfigurableListableBeanFactory getBeanFactory() {
        return beanFactory;
    }
}
